package com.game;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public abstract class BasicButton 
{
	protected enum State {Unselected, Selected, Used, Disabled};
	
	protected State m_State; 
	
	protected Bitmap m_off, m_on;
	protected float m_x, m_y;
	
	protected int m_ID;
	
	public BasicButton(int id, float x, float y, Bitmap onBitmap, Bitmap offBitmap)
	{
		m_ID = id;
		
		m_State = State.Unselected;
		
		m_off = offBitmap;
		m_on = onBitmap;		
		
		m_x = x;
		m_y = y;		
	}
	
	public void Draw(Canvas canvas)
	{
		canvas.drawBitmap(GetCurrentBitmap(),	m_x, m_y, null);
	}
	
	protected abstract Bitmap GetCurrentBitmap();
	
	public boolean TryTouch(int x, int y)
	{

		int right = GetCurrentBitmap().getWidth() + (int)m_x;
		int down = GetCurrentBitmap().getHeight() + (int)m_y;
		
		boolean touched = true;
		
		if(x> right) touched = false;
		if(x< m_x) touched = false;
		if(y < m_y) touched = false;
		if(y> down) touched = false;
		
		if(touched)
		{
			SelectTouchedState();
		}
		else 
		{
			m_State = GetDefaultState();
		}
		
		return touched;
	}
	
	protected abstract void SelectTouchedState();
	
	protected State GetDefaultState()
	{
		if(m_State == State.Disabled) return State.Disabled;
		else	return State.Unselected;
	}
	
	public boolean Used()
	{
		return (m_State == State.Used);
	}


	public int getID() {
		return m_ID;
	}
	
	public void Enable() { m_State = State.Unselected; }
	public void Disable() { m_State = State.Disabled; }	
	public void Unselect() {m_State = State.Unselected; }
	
	
}
